package model;

import java.util.List;
import java.util.Vector;

import lock.ConcreteLock;
import lock.Lock;

public class Buffer<Integer> {
	private final List<Integer> data;
	private final Lock lock;
	private final Lock criticalSection;
	
	public Buffer(){
		this.data = new Vector<Integer>();
		this.lock = new ConcreteLock(true);
		this.criticalSection = new ConcreteLock(false);
	}

	public void put(final Integer integer) {
		this.criticalSection.lock();
		this.data.add(integer);
		this.lock.unlock();
		this.criticalSection.unlock();
	}

	public Object get() {
		this.lock.lock();
		this.criticalSection.lock();
		final Integer result = this.data.remove(0);
		if (!data.isEmpty()) this.lock.unlock();
		this.criticalSection.unlock();
		return result;
	}
	
	public void iterator(){
		
		this.data.iterator();
		
	}
}
